package cn.edu.neusoft.food729.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.retroiftdemo_729.R;

import java.util.ArrayList;
import java.util.List;

import cn.edu.neusoft.food729.Bean.ShopBean;
import cn.edu.neusoft.food729.adapter.ShopAdapter;
import cn.edu.neusoft.food729.listener.ShopListener;
import cn.edu.neusoft.food729.model.ShopModel;

public class Fragment1 extends BaseFragment {

    private int page = 1;
    private int lastVisibleItemPosition;

    private List<ShopBean> list;
    private LinearLayoutManager layoutManager;
    private ShopAdapter adapter;
    private RecyclerView recyclerView;


    private ShopListener listener = new ShopListener() {
        @Override
        public void onResponse(List<ShopBean> beanlist) {
            if (page == 1) {
                list = beanlist;
            } else {
                list.removeAll(beanlist);
                list.addAll(beanlist);
            }
            adapter = new ShopAdapter(context, list);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, "失败" + msg, Toast.LENGTH_SHORT).show();
        }
    };

    public Fragment1() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        return super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.fragment1, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        System.out.println("--f1--" + getSessionId());//父类

//        Log.i("LoginActivity2333", "----listener=" + listener);
//        Log.i("LoginActivity2333", "----list=" + list);
        initRecycleView(view);
        ShopModel model = new ShopModel();
        model.getResultList(listener);
    }

    private void initRecycleView(View view) {
        recyclerView = view.findViewById(R.id.shop_list);
        // 设置布局
        layoutManager = new LinearLayoutManager(context);
        // 设置动画效果
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //准备数据
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
//设置适配器

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (list != null) {
                    if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                        page += 1;
                        ShopModel model = new ShopModel();
                        model.getResultList(listener);
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();//滚动结束后将赋
            }
        });
    }
}

